<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        footer{
            display: flex;
            margin: 15px;
        }
        img{
            width: 60px;
            height: 60px;
        }
        .box footer .d1{
            margin-top: 0px;
        }
        .box footer .d2{
            margin-top: 10px;
        }
        .box footer .right{
            margin-left: 170px;
            margin-top: 20px;
        }
    </style>
</head>

<body>
    <div id="app" class="box">
        <header>已选商品 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span @click="qingkong(ccc)">清空</span></header>
        <footer class="item" v-for="(ccc,index) in shuzu" v-bind:key="index">
            <div class="left">
                <img v-bind:src="ccc.pic" alt="">
            </div>
            <div class="zhong">
                <div class="d1">{{ccc.name}}</div>
                <div class="d2">${{ccc.nameb}} <span>{{ccc.namec}}</span></div>
            </div>
            <div class="right">
                <span @click="jian(ccc)">-</span><span>{{ccc.shuliang}}</span><span @click="jia(ccc)">+</span>
            </div>
        </footer>
        <!-- <button>去结算</button><span>总价{{zongjia}}</span><span>总数量{{zongshuliang}}</span> -->
        <button>去结算</button><span>总价{{zongjia}}</span><span>总数量{{zongshuliang}}</span>
    </div>
</body>

</html>
<script src="../vue-2.5.21.js"></script>
<script>

    //实例化Vue对象
    var v = new Vue({
        el: '#app', 
        data: {
            shuzu: [
                { id: 1, pic: './img/123.jpg', name: '秋刀鱼', nameb: 35, namec: '35',shuliang:1},
                { id: 2, pic: './img/222.jpg', name: '海带鱼', nameb: 45, namec: '35',shuliang:1},
                { id: 3, pic: './img/hua2.jpg', name: '皮皮虾',nameb: 85, namec: '35',shuliang:1},
            ],
            zongshuliang:1,
            zongjia:165

        }, 
        //计算方法
        // computed: {
        //     zongjia(){
        //          return   ((this.shuzu[0].shuliang)*35+(this.shuzu[1].shuliang)*45+(this.shuzu[2].shuliang)*85)
        //     },
        //     zongshuliang(){
        //         return   ((this.shuzu[0].shuliang)+(this.shuzu[1].shuliang)+(this.shuzu[2].shuliang))
        //     }
        // },
        //监听方法
        watch: {
            shuzu:{
                handler(){
                    console.log('已经改变');
                    this.zongjia=((this.shuzu[0].shuliang)*35+(this.shuzu[1].shuliang)*45+(this.shuzu[2].shuliang)*85);
                    this.zongshuliang=((this.shuzu[0].shuliang)+(this.shuzu[1].shuliang)+(this.shuzu[2].shuliang));

                },
                deep:true,
            }
        },
        methods: {
            jian(ccc){
                // console.log('111');
                if(ccc.shuliang<=1){
                    alert('最少是一个')
                }else{
                    ccc.shuliang--
                    // console.log(ccc.shuliang);
                    // console.log(this.shuzu[0].shuliang);
                }
            },
            jia(ccc){
                if(ccc.shuliang>=5){
                    alert('最多五个')
                }else{
                    ccc.shuliang++
                }
            },
            qingkong(ccc){
                // console.log(this.shuzu);
                this.shuzu=[]
                // console.log('333');
                alert('啥也没有了哦')
            },
        },
    })
</script>